﻿package net.wangit.view.element.component;

import net.wangit.MapList;
import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.support.AbstractElementWriter;
import net.wangit.util.Checker;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.element.util.ElementUtil;
import net.wangit.view.enumeration.EnumerationUtil;
import net.wangit.view.util.Html;

public class SelectFieldElementWriter extends AbstractElementWriter {

	public SelectFieldElementWriter() {
	}

	protected void writeText(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		MapList enumeration = (MapList) ec.getObject();
		String value = ElementUtil.getValueOrDefault(ac, ec, row);
		Html.set(html, EnumerationUtil.getEnumerationName(enumeration, value));
	}

	protected void writeTextWithValue(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeText(ac, html, ec, row);
		ElementUtil.setHidden(ac, html, ec, row);
	}

	protected void writeControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		int height = ec.getHeight();
		String attribute;
		if (height > 0)
			attribute = (new StringBuilder("onclick=\"$f43(true,this,"))
					.append(height).append(")\" onkeyup=\"$f43(false,this,")
					.append(height).append(")\"").toString();
		else
			attribute = "onclick=\"$f43(true,this)\" onkeyup=\"$f43(false,this)\"";
		write(ac, html, ec, row, attribute);
	}

	protected void writeReadOnlyControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		String attribute = "readonly onclick=\"$f43(true,this)\"";
		write(ac, html, ec, row, attribute);
	}

	protected void writeDisabledControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeText(ac, html, ec, row);
	}

	protected void writeHidden(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		ElementUtil.setHidden(ac, html, ec, row);
	}

	private void write(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row, String attribute) {
		String name = ElementUtil.getAlias(ec);
		if (row == null || row != null && row.getIndex() <= 0) {
			html.append("<select id=\"").append(name).append(
					"_43src\" style=\"display:none\">");
			writeOptions(ac, html, ec, row);
			html.append("</select>");
		}
		String value = ElementUtil.getValueOrDefaultWithFormat(ac, ec, row);
		html.append("<span onmouseout=\"$f43Out('").append(name).append(
				"','y')\" onmouseover=\"$f43Out('").append(name).append(
				"','n')\">");
		html.append("<input type=\"hidden\" name=\"").append(name).append("\"");
		Html.setValue(html, value);
		html.append("/>");
		html.append("<input type=\"text\" id=\"").append(name).append(
				"_43\" onblur=\"$f43Close(this)\"");
		MapList enumeration = (MapList) ec.getObject();
		Html.setValue(html, EnumerationUtil.getEnumerationName(enumeration,
				value));
		Html.setAttribute(html, attribute);
		Html.setAttribute(html, ElementUtil.resolveCustom(ac, ec, row));
		Html.setCss(html, ec.getCss(), "E43");
		Html.setTitle(html, ElementUtil.resolveTitle(ac, ec, row));
		Html.setStyleWithWidth(html, ec.getWidth());
		html.append(" />");
		html.append("</span>");
	}

	private void writeOptions(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		MapList enumeration = (MapList) ec.getObject();
		if (!Checker.isEmpty(enumeration)) {
			for (int i = 0; i < enumeration.size(); i++) {
				Row item = enumeration.getRow(i);
				html.append("<option value=\"").append(item.get("value"))
						.append("\">").append(item.get("name")).append(
								"</option>");
			}

		}
	}
}
